﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace FastCodeGeneration.Core.Framework
{
    public abstract class BaseServices<R, T> : IBaseServices<T> where R : IBaseRepository<T> where T : class, new()
    {
        protected R _repository;
        protected BaseServices(R _repository)
        {
            this._repository = _repository;
        }

        /// <summary>
        /// 根据主值查询单条数据
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns>泛型实体</returns>
        public T SelectById(dynamic id)
        {
            return _repository.SelectById(id);
        }


        /// <summary>
        /// 根据id集合查询
        /// </summary>
        /// <param name="ids">主键值</param>
        /// <returns>泛型实体</returns>
        public List<T> SelectByIds(IEnumerable<dynamic> ids)
        {
            return _repository.SelectByIds(ids);
        }

        /// <summary>
        /// 查询全部数据
        /// </summary>
        /// <returns></returns>
        public List<T> SelectAll()
        {
            return _repository.SelectAll();
        }


        /// <summary>
        /// 查询count
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <returns></returns>
        public long SelectCount(Expression<Func<T, bool>> predicate)
        {
            return _repository.SelectCount(predicate);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <returns></returns>
        public T SelectOne(Expression<Func<T, bool>> predicate)
        {
            return _repository.SelectOne(predicate);
        }

        /// <summary>
        /// 查询集合
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <returns></returns>
        public List<T> SelectList(Expression<Func<T, bool>> predicate)
        {
            return _repository.SelectList(predicate);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page">分页封装</param>
        /// <param name="queryWrapper">查询条件</param>
        /// <returns></returns>
        public IPage<T> SelectPage(IPage<T> page, QueryWrapper<T> queryWrapper)
        {
            return _repository.SelectPage(page, queryWrapper);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="">前端条件封装</param>
        /// <returns></returns>
        public IPage<T> SelectPage(Condition condition)
        {
            return _repository.SelectPage(condition);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">对象</param>
        /// <returns></returns>
        public T InsertEntity(T entity)
        {
            return _repository.InsertEntity(entity);
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entities">对象集合</param>
        /// <returns>行数</returns>
        public bool BatchInsert(List<T> entities)
        {
            return _repository.BatchInsert(entities);
        }


        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">对象</param>
        /// <returns></returns>
        public bool UpdateEntity(T entity)
        {
            return _repository.UpdateEntity(entity);
        }

        /// <summary>
        /// 批量更新
        /// </summary>
        /// <param name="entities">对象集合</param>
        /// <returns>行数</returns>
        public bool BatchUpdate(List<T> entities)
        {
            return _repository.BatchUpdate(entities);
        }


        /// <summary>
        /// 添加或更新
        /// </summary>
        /// <param name="entity">对象</param>
        /// <returns>行数</returns>
        public bool Save(T entity)
        {
            return _repository.Save(entity);
        }


        /// <summary>
        /// 添加或更新
        /// </summary>
        /// <param name="entities">对象集合</param>
        /// <returns>行数</returns>
        public bool BatchSave(List<T> entities)
        {
            return _repository.BatchSave(entities);
        }


        /// 根据id删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public bool DeleteById(dynamic id)
        {
            return _repository.DeleteById(id);
        }

        /// <summary>
        /// 根据id集合
        /// </summary>
        /// <param name="ids">主键集合</param>
        /// <returns></returns>
        public bool DeleteByIds(IEnumerable<dynamic> ids)
        {
            return _repository.DeleteByIds(ids.ToArray());
        }

        /// <summary>
        /// 根据对象删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool DeleteByEntity(T entity)
        {
            return _repository.Delete(entity);
        }


        /// <summary>
        /// 根据对象集合删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool DeleteByEntities(List<T> entities)
        {
            return _repository.Delete(entities);
        }

    }
}
